import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * durnan.org Greg Durnan January 2026
 * Launcher window for S11 and S22 Smith Chart viewers.
 * 
 * <p>This simple Swing application provides a clean entry point to launch 
 * independent Smith Chart viewer windows for S11 and S22 reflection coefficients.</p>
 * 
 * <p>Features:</p>
 * <ul>
 *   <li>System look-and-feel</li>
 *   <li>Centered window with minimum size</li>
 *   <li>Two large buttons to launch S11 and S22 viewers</li>
 * </ul>
 * 
 * <h2>Usage</h2>
 * <pre>
 * java Launch
 * </pre>
 * <p>Or run directly from an IDE by executing the main method.</p>
 * 
 * <h2>Customization</h2>
 * <p>The only part the user needs to modify is the button action listeners 
 * (already prepared with comments) to instantiate the actual Smith Chart classes:</p>
 * <ul>
 *   <li><code>new SmithChartPlotter11()</code> for S11</li>
 *   <li><code>new SmithChartPlotter22()</code> for S22</li>
 * </ul>
 * 
 * <h2>Producing a Linux/Unix Man Page (groff source)</h2>
 * <p>Copy the following block and save it as e.g. <code>launch-smith.1</code>, then run:</p>
 * <pre>
 * groff -man -Tpdf launch-smith.1 > launch-smith.pdf
 * </pre>
 * <pre>
 * .\" Process this file with: groff -man -Tpdf launch-smith.1 > launch-smith.pdf
 * .TH Launch 1 "January 2026" "v1.0 durnan.org" "User Commands"
 * .SH NAME
 * Launch \- simple graphical launcher for S11 and S22 Smith Chart viewers
 * .SH SYNOPSIS
 * .B java Launch
 * .SH DESCRIPTION
 * This Java Swing application provides a minimal GUI with two buttons:
 * .IP \[bu]
 * "Open S11 Smith Chart" → launches the S11 reflection coefficient viewer"
 * .IP \[bu]
 * "Open S22 Smith Chart" → launches the S22 reflection coefficient viewer"
 * .PP
 * The application uses the system look-and-feel and centers itself on screen.
 * The viewer classes must be available in the classpath and have a default constructor.
 * .SH USAGE
 * Simply run:
 * .nf
 * java Launch
 * .fi
 * .PP
 * Or from an IDE, execute the main method.
 * .SH CUSTOMIZATION
 * The button action listeners are already prepared. Replace the placeholder lines:
 * .nf
 * new SmithChartPlotter11();   // ← change to your actual S11 class
 * new SmithChartPlotter22();   // ← change to your actual S22 class
 * .fi
 * .SH REQUIREMENTS
 * - Java SE 8 or later
 * - The SmithChartPlotter11 and SmithChartPlotter22 classes must exist
 * .SH AUTHOR
 * durnan.org
 * .SH "SEE ALSO"
 * .BR java (1),
 * .BR groff (1)
 * </pre>
 *
 * @durnan.org
 * @version 1.0
 */
public class Launch extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                System.err.println("Look and feel failed: " + e.getMessage());
            }
            new Launch().setVisible(true);
        });
    }

    public Launch() {
        super("S Parameter Viewer");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // Main panel
        JPanel mainPanel = createMainPanel();
        setContentPane(mainPanel);

        pack();
        setLocationRelativeTo(null); // center on screen
        setMinimumSize(new Dimension(360, 280));
    }

    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;

        // Title label
        JLabel titleLabel = new JLabel("s11 & s22 Viewer", SwingConstants.CENTER);
        titleLabel.setFont(new Font("Segoe UI", Font.BOLD, 18));
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        panel.add(titleLabel, gbc);

        // S11 button
        JButton btnS11 = new JButton("Open S11 Smith Chart");
        btnS11.setFont(new Font("Segoe UI", Font.PLAIN, 16));
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.insets = new Insets(20, 10, 10, 10);
        panel.add(btnS11, gbc);

        // S22 button
        JButton btnS22 = new JButton("Open S22 Smith Chart");
        btnS22.setFont(new Font("Segoe UI", Font.PLAIN, 16));
        gbc.gridy = 2;
        gbc.insets = new Insets(10, 10, 20, 10);
        panel.add(btnS22, gbc);

        // === Button Actions ===
        // User code for Button presses - replace class names as needed
        btnS11.addActionListener(e -> {
            // Normal instantiation of the S11 viewer window
            new SmithChartPlotter11(); // ← adjust class name if different
        });

        btnS22.addActionListener(e -> {
            // Normal instantiation of the S22 viewer window
            new SmithChartPlotter22(); // ← adjust class name if different
        });

        return panel;
    }
}